package cn.wolfcode.crm.shiro;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.mapper.RoleMapper;
import cn.wolfcode.crm.service.IEmployeeService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 自定义数据源
 */
@Component("crmRealm")
public class CRMRealm extends AuthorizingRealm {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    /**
     * 提供权限相关
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("=================");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //获得当前登录用户
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        if (employee.isAdmin()) {
            info.addRole("admin");
            info.addStringPermission("*:*");//表示所有权限的意思
        }else {
            //查询当前登录用户有哪些角色编码,哪些权限表达式,全部查询出来然后再添加到list集合中
            List<String> roles =  roleMapper.selectSnByEmpid(employee.getId());
            info.addRoles(roles);
            List<String> permissions = permissionMapper.selectByEmpid(employee.getId());
            info.addStringPermissions(permissions);
        }
        return info;
    }

    /**
     * 提供验证信息认证
     * @param token
     * @return
     * @throws AuthenticationException
     */

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    //在spring容器中找到credentialsMatcher注入进去
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
        super.setCredentialsMatcher(credentialsMatcher);
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //获得用户名
        String principal = (String) token.getPrincipal();
        //查询数据库是否有该账号的数据
        Employee employee = employeeService.checkName(principal);
        if (employee != null) {
            return new SimpleAuthenticationInfo(employee,employee.getPassword(),
                    ByteSource.Util.bytes(employee.getName()),"crmRealm");
        }
        //如果没有账号就返回null
        return null;
    }
}
